package com.zhonghan.common.wechat.api;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.alibaba.fastjson.JSON;
import com.zhonghan.common.utils.HttpClientUtil;
import com.zhonghan.common.wechat.bean.result.BaseResult;
import com.zhonghan.common.wechat.bean.user.BaseUserInfo;
import com.zhonghan.common.wechat.bean.user.Tag;
import com.zhonghan.common.wechat.bean.user.TagResult;
import com.zhonghan.common.wechat.bean.user.UserList;
import com.zhonghan.common.wechat.bean.user.UserTags;

/**
 * 获取用户信息(未完善,未测试)
 * @author zhangceven
 *
 */
public class UserApi {
	
	/**
	 * 获取用户的基本信息
	 * @param access_token
	 * @param openid
	 * @param lang
	 * @return
	 */
	public static BaseUserInfo getUserInfo(String access_token,String openid,String lang){
		
		String url = "https://api.weixin.qq.com/cgi-bin/user/info"
				+ "?access_token=" + access_token
				+ "&openid=" + openid
				+ "&lang=" + lang;
		
		String result = HttpClientUtil.doGet(url);
		
		BaseUserInfo baseUserInfo = JSON.parseObject(result,BaseUserInfo.class);
		
		return baseUserInfo;
		
	}
	
	/**
	 * 创建用户标签(通过Tag对象)
	 * @param access_token
	 * @return
	 */
	public Tag createTags(String access_token,Tag tag){
		
		String tagJson = JSON.toJSONString(tag);
		
		tag = createTags(access_token, tagJson);
		
		return tag;
		
	}
	
	/**
	 * 创建用户标签(通过Tag的json数据)
	 * @param access_token
	 * @return
	 */
	public Tag createTags(String access_token,String tagJson){
		
		String url = "https://api.weixin.qq.com/cgi-bin/tags/create"
				+ "?access_token=" + access_token;
		
		String result = HttpClientUtil.doPostJson(url, tagJson);
		
		Tag tag = JSON.parseObject(result, Tag.class);
		
		return tag;
		
	}
	
	/**
	 * 获取公众号已创建的标签
	 * @param access_token
	 * @return
	 */
	public TagResult getTags(String access_token){
		
		String url = "https://api.weixin.qq.com/cgi-bin/tags/get"
				+ "?access_token=" + access_token;
		
		String result = HttpClientUtil.doGet(url);
		
		TagResult tagResult = JSON.parseObject(result, TagResult.class);
		
		return tagResult;
		
	}
	
	/**
	 * 编辑标签(通过Tag对象)
	 * @param access_token
	 * @param tag
	 * @return
	 */
	public BaseResult updateTags(String access_token,Tag tag){
		
		String tagJson = JSON.toJSONString(tag);
		
		BaseResult baseResult = updateTags(access_token, tagJson );
		
		return baseResult;
		
	}
	
	/**
	 * 编辑标签(通过Tag的json数据)
	 * @param access_token
	 * @param tagJson
	 * @return
	 */
	public BaseResult updateTags(String access_token,String tagJson){
		
		String url = "https://api.weixin.qq.com/cgi-bin/tags/update"
				+ "?access_token=" + access_token;
		
		String result = HttpClientUtil.doPostJson(url, tagJson );
		
		BaseResult baseResult = JSON.parseObject(result, BaseResult.class);
		
		return baseResult;
		
	}
	
	/**
	 * 删除标签(通过Tag对象)
	 * @param access_token
	 * @param tag
	 * @return
	 */
	public BaseResult deleteUserTags(String access_token,Tag tag){
		
		String tagJson = JSON.toJSONString(tag);
		
		BaseResult baseResult = deleteTags(access_token, tagJson);
		
		return baseResult;
		
	}
	
	/**
	 * 删除标签
	 * @param access_token
	 * @param tagJson
	 * @return
	 */
	public BaseResult deleteTags(String access_token,String tagJson){
		
		String url = "https://api.weixin.qq.com/cgi-bin/tags/delete"
				+ "?access_token=" + access_token;
		
		String result = HttpClientUtil.doPostJson(url, tagJson);
		
		BaseResult baseResult = JSON.parseObject(result, BaseResult.class);
		
		return baseResult;
		
	}
	
	/**
	 * 获取标签下粉丝列表
	 * @param access_token
	 * @param tagid
	 * @return
	 */
	public BaseResult getUserTags(String access_token,String tagid){
		
		String url = "https://api.weixin.qq.com/cgi-bin/user/tag/get"
				+ "?access_token=" + access_token;
		
		String result = HttpClientUtil.doGet(url);
		
		UserTags userTags = JSON.parseObject(result, UserTags.class);
		
		return userTags;
		
	}
	
	/*********		标签未完		************/
	
	/**
	 * 设置用户备注名
	 * @param access_token 	调用接口凭证
	 * @param openid 		用户标识
	 * @param remark 		新的备注名，长度必须小于30字符
	 * @return
	 */
	public BaseResult updateremark(String access_token,String openid,String remark){
		
		String url = "https://api.weixin.qq.com/cgi-bin/user/info/updateremark"
				+ "?access_token=" + access_token;
		
		Map<String,String> map = new HashMap<>();
		map.put("openid", openid);
		map.put("remark", remark);
		
		String json = JSON.toJSONString(map);
		
		String result = HttpClientUtil.doPostJson(url, json);
		
		BaseResult baseResult = JSON.parseObject(result, BaseResult.class);
		
		return baseResult;
		
	}
	
	/**
	 * 获取用户列表
	 * @param access_token 调用接口凭证
	 * @param next_openid  第一个拉取的OPENID，不填默认从头开始拉取
	 * @return
	 */
	public BaseResult getUserList(String access_token,String next_openid){
		
		String url = "https://api.weixin.qq.com/cgi-bin/user/get"
				+ "?access_token=" + access_token		
				+ "&next_openid=" + next_openid;
		
		String result = HttpClientUtil.doGet(url);
		
		UserList userList = JSON.parseObject(result,UserList.class);
		
		return userList;
		
	}
	
	/**
	 * 获取公众号的黑名单列表
	 * @param access_token
	 * @param begin_openid
	 * @return
	 */
	public BaseResult getBlackList(String access_token,String begin_openid){
		
		String url = "https://api.weixin.qq.com/cgi-bin/tags/members/getblacklist"
				+ "?access_token=" + access_token;
		
		Map<String,String> map = new HashMap<>();
		map.put("begin_openid", begin_openid);
		
		String json = JSON.toJSONString(map);
		
		String result = HttpClientUtil.doPostJson(url, json);
		
		UserList userList = JSON.parseObject(result, UserList.class);
		
		return userList;
		
	}
	
	/**
	 * 拉黑用户(可批量拉黑)
	 * @param access_token
	 * @param openid_list
	 * @return
	 */
	public BaseResult batchBlackList(String access_token,List<String> openid_list){
		
		String url = "https://api.weixin.qq.com/cgi-bin/tags/members/batchblacklist"
				+ "?access_token=" + access_token;
		
		String json = JSON.toJSONString(openid_list);
		
		String result = HttpClientUtil.doPostJson(url, json);
		
		BaseResult baseResult = JSON.parseObject(result, BaseResult.class);
		
		return baseResult;
		
	}
	
	/**
	 * 取消拉黑用户(可批量取消)
	 * @param access_token
	 * @param openid_list
	 * @return
	 */
	public BaseResult batchUnBlackList(String access_token,List<String> openid_list){
		
		String url = "https://api.weixin.qq.com/cgi-bin/tags/members/batchunblacklist"
				+ "?access_token=" + access_token;
		
		String json = JSON.toJSONString(openid_list);		
		
		String result = HttpClientUtil.doPostJson(url, json);
		
		BaseResult baseResult = JSON.parseObject(result, BaseResult.class);
		
		return baseResult;
		
	}
	
	
	
}
